package cn.yunyichina.provider.base.service.impl;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2017/7/21.
 */
public class FileServiceImpl {

    public static void readFile(String sourceFilePath, String encode) throws IOException {
//        File file = new File(sourceFilePath);
//        BufferedReader br = new   BufferedReader(new InputStreamReader(new FileInputStream(file), encode));
//        StringBuilder strBuilder = new StringBuilder();
//        String sLine = null;
//        while((sLine = br.readLine()) != null){
//            strBuilder.append(sLine);
//            strBuilder.append("\r\n");
//        }
//        br.close();
//        System.out.println(strBuilder.toString());
    }

    public static void main(String[] args) throws IOException {
//        URL urlfile = new URL("http://opgl8aq15.bkt.clouddn.com/%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6%E6%B7%B1%E5%9C%B3%E5%8C%BB%E9%99%A2_%E6%94%B6%E5%8D%95%E6%9C%BA%E6%9E%84%E8%AE%A2%E5%8D%95%E6%B5%81%E6%B0%B4%E6%98%8E%E7%BB%86.csv?attname=&e=1500614221&token=6MpX59besahQ1TEFbRRd0hGEr8Alf1OMWP9Vr9Pp:cKAzLSXP9sdAvqmH1P0nfnRNEp0");
//        BufferedReader in = new BufferedReader(new InputStreamReader(urlfile.openStream()));
//        String content="";
//        String inputLine =in.readLine();
//        while(inputLine!=null){
//            content +=inputLine;
//            inputLine =in.readLine();
//        }
//        System.out.println(content);
//        in.close();
        downloadFile("http://opgl8aq15.bkt.clouddn.com/%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6%E6%B7%B1%E5%9C%B3%E5%8C%BB%E9%99%A2_%E6%94%B6%E5%8D%95%E6%9C%BA%E6%9E%84%E8%AE%A2%E5%8D%95%E6%B5%81%E6%B0%B4%E6%98%8E%E7%BB%86.csv?attname=&e=1500614221&token=6MpX59besahQ1TEFbRRd0hGEr8Alf1OMWP9Vr9Pp:cKAzLSXP9sdAvqmH1P0nfnRNEp0",
                "D:\\20170721.cvs");
    }

    public static void downloadFile(String remoteFilePath, String localFilePath)
    {
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File f = new File(localFilePath);
        try
        {
            urlfile = new URL(remoteFilePath);
            httpUrl = (HttpURLConnection)urlfile.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());
            bos = new BufferedOutputStream(new FileOutputStream(f));
            int len = 2048;
            byte[] b = new byte[len];
            while ((len = bis.read(b)) != -1)
            {
                bos.write(b, 0, len);
            }
            bos.flush();
            bis.close();
            httpUrl.disconnect();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                bis.close();
                bos.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

}
